home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / prsr_lib / attribut.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-10  |  25.2 KB  |  505 lines

  1. /*****************************************************************************
  2. * Setting attributes for geometric objects.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.2, Mar. 1990   *
  5. *****************************************************************************/
  6.  
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include "irit_sm.h"
  11. #include "iritprsr.h"
  12. #include "attribut.h"
  13. #include "allocate.h"
  14.  
  15. /*****************************************************************************
  16. * DESCRIPTION:                                                               M
  17. * Routine to set the color of an object.                     M
  18. *                                                                            *
  19. * PARAMETERS:                                                                M
  20. *   PObj:      Object to set its color to Color.                             M
  21. *   Color:     New color for PObj.                                           M
  22. *                                                                            *
  23. * RETURN VALUE:                                                              M
  24. *   void                                                                     M
  25. *                                                                            *
  26. * KEYWORDS:                                                                  M
  27. *   AttrSetObjectColor, attributes, color                                    M
  28. *****************************************************************************/
  29. void AttrSetObjectColor(IPObjectStruct *PObj, int Color)
  30. {
  31.     AttrSetObjectIntAttrib(PObj, "color", Color);
  32. }
  33.  
  34. /*****************************************************************************
  35. * DESCRIPTION:                                                               M
  36. * Routine to return the color of an object.                     M
  37. *                                                                            M
  38. *                                                                            *
  39. * PARAMETERS:                                                                M
  40. *   PObj:     For which we would like to know the color of.                  M
  41. *                                                                            *
  42. * RETURN VALUE:                                                              M
  43. *   int:      Color of PObj or IP_ATTR_NO_COLOR if no color set.             M
  44. *                                                                            *
  45. * KEYWORDS:                                                                  M
  46. *   AttrGetObjectColor, attributes, color                                    M
  47. *****************************************************************************/
  48. int AttrGetObjectColor(IPObjectStruct *PObj)
  49. {
  50.     IPAttributeStruct
  51.     *Attr = AttrFindAttribute(PObj -> Attrs, "color");
  52.  
  53.     if (Attr != NULL) {
  54.     if (Attr -> Type == IP_ATTR_INT)
  55.         return Attr -> U.I;
  56.     else if (Attr -> Type == IP_ATTR_STR)
  57.         return atoi(Attr -> U.Str);
  58.     else
  59.         return IP_ATTR_NO_COLOR;
  60.     }
  61.     else
  62.         return IP_ATTR_NO_COLOR;
  63. }
  64.  
  65. /*****************************************************************************
  66. * DESCRIPTION:                                                               M
  67. * Routine to set the RGB color of an object.                     M
  68. *                                                                            *
  69. * PARAMETERS:                                                                M
  70. *   PObj:               Object to set its RGB color.                         M
  71. *   Red, Green, Blue:   Component of color.                                  M
  72. *                                                                            *
  73. * RETURN VALUE:                                                              M
  74. *   void                                                                     M
  75. *                                                                            *
  76. * KEYWORDS:                                                                  M
  77. *   AttrSetObjectRGBColor, attributes, color, rgb                            M
  78. *****************************************************************************/
  79. void AttrSetObjectRGBColor(IPObjectStruct *PObj, int Red, int Green, int Blue)
  80. {
  81.     char SRGB[30];
  82.  
  83.     sprintf(SRGB, "%d,%d,%d", Red, Green, Blue);
  84.  
  85.     AttrSetObjectStrAttrib(PObj, "rgb", SRGB);
  86. }
  87.  
  88. /*****************************************************************************
  89. * DESCRIPTION:                                                               M
  90. * Routine to return the RGB color of an object.                          M
  91. *                                                                            *
  92. * PARAMETERS:                                                                M
  93. *   PObj:               Object to get its RGB color.                         M
  94. *   Red, Green, Blue:   Component of color to initialize.                    M
  95. *                                                                            *
  96. * RETURN VALUE:                                                              M
  97. *   int:     TRUE if PObj does have an RGB color attribute, FALSE otherwise. M
  98. *                                                                            *
  99. * KEYWORDS:                                                                  M
  100. *   AttrGetObjectRGBColor, attributes, color, rgb                            M
  101. *                                                                            M
  102. *****************************************************************************/
  103. int AttrGetObjectRGBColor(IPObjectStruct *PObj,
  104.               int *Red,
  105.               int *Green,
  106.               int *Blue)
  107. {
  108.     char
  109.     *p = AttrGetStrAttrib(PObj -> Attrs, "rgb");
  110.  
  111.     return p != NULL && sscanf(p, "%d,%d,%d", Red, Green, Blue) == 3;
  112. }
  113.  
  114. /*****************************************************************************
  115. * DESCRIPTION:                                                               M
  116. * Routine to set an integer attribute for an object.                 M
  117. *                                                                            *
  118. * PARAMETERS:                                                                M
  119. *   PObj:     To add an integer attribute for.                               M
  120. *   Name:     Name of attribute.                                             M
  121. *   Data:     Content of attribute.                                          M
  122. *                                                                            *
  123. * RETURN VALUE:                                                              M
  124. *   void                                                                     M
  125. *                                                                            *
  126. * KEYWORDS:                                                                  M
  127. *   AttrSetObjectIntAttrib, attributes                                       M
  128. *****************************************************************************/
  129. void AttrSetObjectIntAttrib(IPObjectStruct *PObj, char *Name, int Data)
  130. {
  131.     int i;
  132.     IPObjectStruct *PObjTmp;
  133.  
  134.     if (IP_IS_OLST_OBJ(PObj)) {
  135.     for (i = 0; (PObjTmp = ListObjectGet(PObj, i)) != NULL; i++)
  136.         AttrSetObjectIntAttrib(PObjTmp, Name, Data);
  137.     }
  138.     else {
  139.     AttrSetIntAttrib(&PObj -> Attrs, Name, Data);
  140.     }
  141. }
  142.  
  143. /*****************************************************************************
  144. * DESCRIPTION:                                                               M
  145. * Routine to get an integer attribute from an object.                 M
  146. *                                                                            *
  147. * PARAMETERS:                                                                M
  148. *   PObj:     Object from which to get an ingeter attribute.                 M
  149. *   Name:     Name of integer attribute.                                     M
  150. *                                                                            *
  151. * RETURN VALUE:                                                              M
  152. *   int:      Found attribute, or IP_ATTR_BAD_INT if not found.              M
  153. *                                                                            *
  154. * KEYWORDS:                                                                  M
  155. *   AttrGetObjectIntAttrib, attributes                                       M
  156. *****************************************************************************/
  157. int AttrGetObjectIntAttrib(IPObjectStruct *PObj, char *Name)
  158. {
  159.     return AttrGetIntAttrib(PObj -> Attrs, Name);
  160. }
  161.  
  162. /*****************************************************************************
  163. * DESCRIPTION:                                                               M
  164. * Routine to set a pointer attribute for an object.                 M
  165. *                                                                            *
  166. * PARAMETERS:                                                                M
  167. *   PObj:     To add a pointer attribute for.                                M
  168. *   Name:     Name of attribute.                                             M
  169. *   Data:     Content of attribute.                                          M
  170. *                                                                            *
  171. * RETURN VALUE:                                                              M
  172. *   void                                                                     M
  173. *                                                                            *
  174. * KEYWORDS:                                                                  M
  175. *   AttrSetObjectPtrAttrib, attributes                                       M
  176. *****************************************************************************/
  177. void AttrSetObjectPtrAttrib(IPObjectStruct *PObj, char *Name, VoidPtr Data)
  178. {
  179.     int i;
  180.     IPObjectStruct *PObjTmp;
  181.  
  182.     if (IP_IS_OLST_OBJ(PObj)) {
  183.     for (i = 0; (PObjTmp = ListObjectGet(PObj, i)) != NULL; i++)
  184.         AttrSetObjectPtrAttrib(PObjTmp, Name, Data);
  185.     }
  186.     else {
  187.     AttrSetPtrAttrib(&PObj -> Attrs, Name, Data);
  188.     }
  189. }
  190.  
  191. /*****************************************************************************
  192. * DESCRIPTION:                                                               M
  193. * Routine to get a pointer attribute from an object.                 M
  194. *                                                                            *
  195. * PARAMETERS:                                                                M
  196. *   PObj:     Object from which to get a pointer attribute.                  M
  197. *   Name:     Name of pointer attribute.                                     M
  198. *                                                                            *
  199. * RETURN VALUE:                                                              M
  200. *   VoidPtr:  Found attribute, or NULL if not found.                      M
  201. *                                                                            *
  202. * KEYWORDS:                                                                  M
  203. *   AttrGetObjectPtrAttrib, attributes                                       M
  204. *****************************************************************************/
  205. VoidPtr AttrGetObjectPtrAttrib(IPObjectStruct *PObj, char *Name)
  206. {
  207.     return AttrGetPtrAttrib(PObj -> Attrs, Name);
  208. }
  209.  
  210. /*****************************************************************************
  211. * DESCRIPTION:                                                               M
  212. * Routine to set a real attribute for an object.                 M
  213. *                                                                            *
  214. * PARAMETERS:                                                                M
  215. *   PObj:     To add a real attribute for.                               M
  216. *   Name:     Name of attribute.                                             M
  217. *   Data:     Content of attribute.                                          M
  218. *                                                                            *
  219. * RETURN VALUE:                                                              M
  220. *   void                                                                     M
  221. *                                                                            *
  222. * KEYWORDS:                                                                  M
  223. *   AttrSetObjectRealAttrib, attributes                                      M
  224. *****************************************************************************/
  225. void AttrSetObjectRealAttrib(IPObjectStruct *PObj, char *Name, RealType Data)
  226. {
  227.     int i;
  228.     IPObjectStruct *PObjTmp;
  229.  
  230.     if (IP_IS_OLST_OBJ(PObj)) {
  231.     for (i = 0; (PObjTmp = ListObjectGet(PObj, i)) != NULL; i++)
  232.         AttrSetObjectRealAttrib(PObjTmp, Name, Data);
  233.     }
  234.     else {
  235.     AttrSetRealAttrib(&PObj -> Attrs, Name, Data);
  236.     }
  237. }
  238.  
  239. /*****************************************************************************
  240. * DESCRIPTION:                                                               M
  241. * Routine to get a real attribute from an object.                 M
  242. *                                                                            *
  243. * PARAMETERS:                                                                M
  244. *   PObj:     Object from which to get a real attribute.                     M
  245. *   Name:     Name of real attribute.                                        M
  246. *                                                                            *
  247. * RETURN VALUE:                                                              M
  248. *   RealType:  Found attribute, or IP_ATTR_BAD_REAL if not found.          M
  249. *                                                                            *
  250. * KEYWORDS:                                                                  M
  251. *   AttrGetObjectRealAttrib, attributes                                      M
  252. *****************************************************************************/
  253. RealType AttrGetObjectRealAttrib(IPObjectStruct *PObj, char *Name)
  254. {
  255.     return AttrGetRealAttrib(PObj -> Attrs, Name);
  256. }
  257.  
  258. /*****************************************************************************
  259. * DESCRIPTION:                                                               M
  260. * Routine to set a string attribute for an object.                 M
  261. *                                                                            *
  262. * PARAMETERS:                                                                M
  263. *   PObj:     To add a string attribute for.                                 M
  264. *   Name:     Name of attribute.                                             M
  265. *   Data:     Content of attribute.                                          M
  266. *                                                                            *
  267. * RETURN VALUE:                                                              M
  268. *   void                                                                     M
  269. *                                                                            *
  270. * KEYWORDS:                                                                  M
  271. *   AttrSetObjectStrAttrib, attributes                                       M
  272. *****************************************************************************/
  273. void AttrSetObjectStrAttrib(IPObjectStruct *PObj, char *Name, char *Data)
  274. {
  275.     int i;
  276.     IPObjectStruct *PObjTmp;
  277.  
  278.     if (IP_IS_OLST_OBJ(PObj)) {
  279.     for (i = 0; (PObjTmp = ListObjectGet(PObj, i)) != NULL; i++)
  280.         AttrSetObjectStrAttrib(PObjTmp, Name, Data);
  281.     }
  282.     else {
  283.     AttrSetStrAttrib(&PObj -> Attrs, Name, Data);
  284.     }
  285. }
  286.  
  287. /*****************************************************************************
  288. * DESCRIPTION:                                                               M
  289. * Routine to get a string attribute from an object.                 M
  290. *                                                                            *
  291. * PARAMETERS:                                                                M
  292. *   PObj:     Object from which to get a string attribute.                   M
  293. *   Name:     Name of string attribute.                                      M
  294. *                                                                            *
  295. * RETURN VALUE:                                                              M
  296. *   char *:      Found attribute, or NULL if not found.                  M
  297. *                                                                            *
  298. * KEYWORDS:                                                                  M
  299. *   AttrGetObjectStrAttrib, attributes                                       M
  300. *****************************************************************************/
  301. char *AttrGetObjectStrAttrib(IPObjectStruct *PObj, char *Name)
  302. {
  303.     return AttrGetStrAttrib(PObj -> Attrs, Name);
  304. }
  305.  
  306. /*****************************************************************************
  307. * DESCRIPTION:                                                               M
  308. * Routine to set an object attribute for an object.                 M
  309. *                                                                            *
  310. * PARAMETERS:                                                                M
  311. *   PObj:     To add an object attribute for.                                M
  312. *   Name:     Name of attribute.                                             M
  313. *   Data:     Content of attribute.                                          M
  314. *   CopyData: If TRUE, Data object is duplicated first.                 M
  315. *                                                                            *
  316. * RETURN VALUE:                                                              M
  317. *   void                                                                     M
  318. *                                                                            *
  319. * KEYWORDS:                                                                  M
  320. *   AttrSetObjectObjAttrib, attributes                                       M
  321. *****************************************************************************/
  322. void AttrSetObjectObjAttrib(IPObjectStruct *PObj,
  323.                 char *Name,
  324.                 IPObjectStruct *Data,
  325.                 int CopyData)
  326. {
  327.     int i;
  328.     IPObjectStruct *PObjTmp;
  329.  
  330.     if (IP_IS_OLST_OBJ(PObj)) {
  331.     /* Must copy attribute if set on a list. */
  332.     for (i = 0; (PObjTmp = ListObjectGet(PObj, i)) != NULL; i++)
  333.         AttrSetObjectObjAttrib(PObjTmp, Name, Data, TRUE);
  334.     if (!CopyData)
  335.         IPFreeObject(Data);
  336.     }
  337.     else {
  338.     AttrSetObjAttrib(&PObj -> Attrs, Name, Data, CopyData);
  339.     }
  340. }
  341.  
  342. /*****************************************************************************
  343. * DESCRIPTION:                                                               M
  344. * Routine to set an object attribute.                         M
  345. *                                                                            *
  346. * PARAMETERS:                                                                M
  347. *   Attrs:     Attribute list where to place new attribute.                  M
  348. *   Name:      Name of thely introducedattribute                             M
  349. *   Data:      Pointer attribute to save.                                    M
  350. *   CopyData:  If TRUE, object Data is duplicated first.                     M
  351. *                                                                            *
  352. * RETURN VALUE:                                                              M
  353. *   void                                                                     M
  354. *                                                                            *
  355. * KEYWORDS:                                                                  M
  356. *   AttrSetObjAttrib, attributes                                             M
  357. *****************************************************************************/
  358. void AttrSetObjAttrib(IPAttributeStruct **Attrs,
  359.               char *Name,
  360.               IPObjectStruct *Data,
  361.               int CopyData)
  362. {
  363.     IPAttributeStruct
  364.         *Attr = AttrFindAttribute(*Attrs, Name);
  365.  
  366.     if (Attr) {
  367.     _AttrFreeAttributeData(Attr);
  368.     Attr -> U.PObj = CopyData ? CopyObject(NULL, Data, TRUE) : Data;
  369.     Attr -> U.PObj -> Pnext = NULL; 
  370.     Attr -> Type = IP_ATTR_OBJ;
  371.     }
  372.     else {
  373.     Attr = _AttrMallocAttribute(Name, IP_ATTR_OBJ);
  374.     Attr -> U.PObj = CopyData ? CopyObject(NULL, Data, TRUE) : Data;
  375.     Attr -> U.PObj -> Pnext = NULL; 
  376.     Attr -> Pnext = *Attrs;
  377.     *Attrs = Attr;
  378.     }
  379. }
  380.  
  381. /*****************************************************************************
  382. * DESCRIPTION:                                                               M
  383. * Routine to get an object attribute from an object.                 M
  384. *                                                                            *
  385. * PARAMETERS:                                                                M
  386. *   PObj:     Object from which to get a object attribute.                   M
  387. *   Name:     Name of object attribute.                                      M
  388. *                                                                            *
  389. * RETURN VALUE:                                                              M
  390. *   IPObjectStruct *:  Found attribute, or NULL if not found.              M
  391. *                                                                            *
  392. * KEYWORDS:                                                                  M
  393. *   AttrGetObjectObjAttrib, attributes                                       M
  394. *****************************************************************************/
  395. IPObjectStruct *AttrGetObjectObjAttrib(IPObjectStruct *PObj, char *Name)
  396. {
  397.     return AttrGetObjAttrib(PObj -> Attrs, Name);
  398. }
  399.  
  400. /*****************************************************************************
  401. * DESCRIPTION:                                                               M
  402. * Routine to get an object attribute.                         M
  403. *                                                                            *
  404. * PARAMETERS:                                                                M
  405. *   Attrs:    Attribute list to search for requested attribute.              M
  406. *   Name:     Name of requested attribute.                                   M
  407. *                                                                            *
  408. * RETURN VALUE:                                                              M
  409. *   IPObjectStruct *:  Found attribute, or NULL if not found.                M
  410. *                                                                            *
  411. * KEYWORDS:                                                                  M
  412. *   AttrGetObjAttrib, attributes                                             M
  413. *****************************************************************************/
  414. IPObjectStruct *AttrGetObjAttrib(IPAttributeStruct *Attrs, char *Name)
  415. {
  416.     IPAttributeStruct
  417.     *Attr = AttrFindAttribute(Attrs, Name);
  418.  
  419.     if (Attr != NULL && Attr -> Type == IP_ATTR_OBJ)
  420.         return Attr -> U.PObj;
  421.     else
  422.         return NULL;
  423. }
  424.  
  425. /*****************************************************************************
  426. * DESCRIPTION:                                                               *
  427. * Routine to release the data slot of an attribute.                 *
  428. *   This routine also exists in miscatt2.c without object handling.         *
  429. *   The routine in miscatt2.c will be linked in iff no object handling is    *
  430. *  used (i.e. this file is not linked in).                     *
  431. *                                                                            *
  432. * PARAMETERS:                                                                *
  433. *   Attr:     To free.                                                       *
  434. *                                                                            *
  435. * RETURN VALUE:                                                              *
  436. *   void                                                                     *
  437. *****************************************************************************/
  438. void _AttrFreeAttributeData(IPAttributeStruct *Attr)
  439. {
  440.     switch (Attr -> Type) {
  441.     case IP_ATTR_INT:
  442.         break;
  443.     case IP_ATTR_REAL:
  444.         break;
  445.     case IP_ATTR_STR:
  446.         IritFree((VoidPtr) Attr -> U.Str);
  447.         break;
  448.     case IP_ATTR_PTR:
  449.         IritFree((VoidPtr) Attr -> U.Ptr);
  450.         break;
  451.     case IP_ATTR_OBJ:
  452.         IPFreeObject(Attr -> U.PObj);
  453.         break;
  454.     default:
  455.         IritPrsrFatalError("Undefined attribute type");
  456.         break;
  457.     }
  458. }
  459.  
  460. /*****************************************************************************
  461. * DESCRIPTION:                                                               *
  462. * Routine to copy one attribute.                         *
  463. *   This routine also exists in miscatt3.c without object handling.         *
  464. *   The routine in miscatt3.c will be linked in iff no object handling is    *
  465. *  used (i.e. this file is not linked in).                     *
  466. *                                                                            *
  467. * PARAMETERS:                                                                *
  468. *   Src:       Attribute to duplicate.                                       *
  469. *                                                                            *
  470. * RETURN VALUE:                                                              *
  471. *   IPAttributeStruct *:   Duplicated attribute.                             *
  472. *****************************************************************************/
  473. IPAttributeStruct *AttrCopyOneAttribute(IPAttributeStruct *Src)
  474. {
  475.     IPAttributeStruct *Dest;
  476.  
  477.     if (Src -> Name[0] == '_')           /* Do not copy internal attributes. */
  478.     return NULL;
  479.  
  480.     Dest = _AttrMallocAttribute(Src -> Name, Src -> Type);
  481.  
  482.     switch (Src -> Type) {
  483.         case IP_ATTR_INT:
  484.         Dest -> U.I = Src -> U.I;
  485.         break;
  486.     case IP_ATTR_REAL:
  487.         Dest -> U.R = Src -> U.R;
  488.         break;
  489.     case IP_ATTR_STR:
  490.         Dest -> U.Str = IritStrdup(Src -> U.Str);
  491.         break;
  492.     case IP_ATTR_OBJ:
  493.         Dest -> U.PObj = CopyObject(NULL, Src -> U.PObj, TRUE);
  494.         break;
  495.     case IP_ATTR_PTR:
  496.         IritPrsrFatalError("Attempt to copy a pointer attribute");
  497.         break;
  498.     default:
  499.         IritPrsrFatalError("Undefined attribute type");
  500.         break;
  501.     }
  502.  
  503.     return Dest;
  504. }
  505.